package com.dhms.uikit.select_bottom_sheet

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.dhms.uikit.databinding.SelectListItemBinding

class SelectListAdapter(
    private val isMultiselectMode: Boolean,
    private val submitSelectedList: (selectedItemList: Set<SelectItem>) -> Unit
) :
    ListAdapter<SelectItem, SelectListAdapter.SelectListViewHolder>(
        DiffCallback
    ) {

    private var _selectedItemList = mutableSetOf<SelectItem>()
    val selectedItemList get() = _selectedItemList

    inner class SelectListViewHolder(var binding: SelectListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: SelectItem, selected: Boolean) {
            binding.name = item.name
            binding.selected = selected
            binding.isMultiselectMode = isMultiselectMode
            binding.executePendingBindings()
        }
    }

    companion object DiffCallback : DiffUtil.ItemCallback<SelectItem>() {
        const val TAG = "PersonListAdapter"

        override fun areItemsTheSame(
            oldItem: SelectItem,
            newItem: SelectItem
        ): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: SelectItem,
            newItem: SelectItem
        ): Boolean {
            return oldItem == newItem
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): SelectListViewHolder {
        return SelectListViewHolder(
            SelectListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun onBindViewHolder(holder: SelectListViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item, hasItemSelected(item))

        holder.binding.itemWrapper.setOnClickListener {
            if (isMultiselectMode) {
                holder.binding.itemCheckbox.apply {
                    isChecked = !isChecked
                }
            } else {
                submitSelectedList(setOf(item))
            }
        }
        holder.binding.itemCheckbox.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                _selectedItemList.add(item)
                Log.d(TAG, "The person ${item.name} is selected.")
            } else {
                //_selectedItemList.remove(item)
                _selectedItemList.removeIf { it.id == item.id }
                Log.d(TAG, "The record ${item.name} is unselected.")
            }
        }
    }

    override fun onViewRecycled(holder: SelectListViewHolder) {
        holder.binding.itemCheckbox.setOnCheckedChangeListener(null)
        super.onViewRecycled(holder)
    }

    fun setSelectedList(itemList: Set<SelectItem>?) {
        _selectedItemList =
            if (!itemList.isNullOrEmpty()) itemList.toMutableSet() else mutableSetOf()
    }

    private fun hasItemSelected(item: SelectItem): Boolean {
        return selectedItemList.firstOrNull { it.id == item.id } != null
    }
}